<script setup lang="ts">
import {ShoppingCart, Menu as MenuIcon, Close as CloseIcon, Search, ArrowDown} from '@element-plus/icons-vue'
import {inject, ref, watch, type Ref} from 'vue'
import {useRouter, useRoute} from 'vue-router'

const router = useRouter()
const route = useRoute()
const isMobileMenuOpen = ref(false)
//分类数据模型
const categoryList = ref<any[]>([])
const toggleMobileMenu = () => {
  isMobileMenuOpen.value = !isMobileMenuOpen.value
}
const searcHK$eyword = ref('')
const searchType = ref('Sp')

// 搜索类型选项
const searchTypes = [
  {value: 'Sp', label: '商品'},
  {value: 'DD', label: '訂單'}
]

const goHome = () => {
  router.push('/')
}

// 分类
const goClassification = (categoryId?: number) => {
  router.push({
    path: '/classification',
    query: {categoryId: categoryId}
  })
}

//搜索
const goSearch = () => {
  switch (searchType.value) {
    case 'Sp':
      router.push({
        path: '/search',
        query: {
          keyword: searcHK$eyword.value,
          type: searchType.value
        }
      })
      break
    case 'DD':
      router.push({
        path: '/dd',
        query: {
          keyword: searcHK$eyword.value,
          type: searchType.value
        }
      })
      break
  }
}

//购物车
const goCart = () => {
  router.push('/cart')
}

/**
 * 获取分类数据
 * 从layout组件注入数据
 */
const mainData = inject<Ref<any>>('main', ref(null))
const processData = (res: any) => {
  if (res && Array.isArray(res.TypeList)) {
    categoryList.value = res.TypeList.map((item: any) => ({
      id: item.TypeId,
      name: item.TypeName
    }))
  }
}

// 件监听，并在初次立即执行
watch(mainData, (val) => {
  processData(val)
}, {immediate: true})

watch(() => route.fullPath, () => {
  isMobileMenuOpen.value = false
})

const isActive = (nameOrPath: string) => {
  return route.name === nameOrPath || route.path === nameOrPath
}
</script>

<template>
  <div class="navbar">
    <!-- 左侧：移动端菜单按钮 -->
    <div class="left-group">
      <button class="menu-toggle" @click="toggleMobileMenu" aria-label="切换菜单">
        <el-icon :size="24">
          <component :is="isMobileMenuOpen ? CloseIcon : MenuIcon"/>
        </el-icon>
      </button>

      <div class="logo">
<!--        <div class="logo-text" @click="goHome">-->
<!--          <span class="logo-text">信億名茶莊</span>-->
<!--        </div>-->
      </div>
    </div>

    <nav class="nav-links" :class="{ open: isMobileMenuOpen }">
      <span class="nav-link dropdown-toggle" :class="{ active: isActive('Home') }" @click="goHome">首頁</span>
      <div class="dropdown">
        <span class="nav-link dropdown-toggle" :class="{ active: isActive('Classification') }">
          商品分類
          <i class="fas fa-chevron-down"></i>
        </span>
        <div class="dropdown-menu">
          <span v-for="category in categoryList" :key="category.id"
                @click="goClassification(category.id)">{{ category.name }}</span>
        </div>
      </div>
    </nav>

    <div class="cart-icon">
      <div class="search-box">
        <el-icon class="search-icon">
          <Search/>
        </el-icon>
        <input type="text" placeholder="搜索商品/訂單" class="search-input" v-model="searcHK$eyword"
               @keyup.enter="goSearch()">
        <div class="search-type-selector">
          <el-dropdown v-model="searchType">
            <span class="el-dropdown-link">
              {{ searchTypes.find(type => type.value === searchType)?.label || '商品' }}
              <el-icon class="el-icon--right">
                <arrow-down/>
              </el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item
                    v-for="type in searchTypes"
                    :key="type.value"
                    @click="searchType = type.value"
                >
                  {{ type.label }}
                </el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
        <button class="search-btn" @click="goSearch()">搜索</button>
      </div>
      <el-button link @click="goCart()">
        <el-icon :size="30" color="dodgerblue" >
          <ShoppingCart/>
        </el-icon>
      </el-button>
    </div>
  </div>
</template>

<style scoped>
.navbar {
  display: flex;
  justify-content: space-between;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
  align-items: center;
  position: relative;
  background-color: #1a252f;
  z-index: 1001;
  padding: 0 40px;
  height: 86px;
}

.left-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.menu-toggle {
  display: none;
  border: none;
  background: transparent;
  padding: 6px;
  border-radius: 6px;
  cursor: pointer;
}

.menu-toggle:hover {
  background: #f5f5f5;
}

.logo {
  display: flex;
  align-items: center;
}

.logo-text {
  text-align: left;
  font-size: 46px;
  margin: 10px;
  color: white;
  font-weight: bold;
}

.nav-links {
  display: flex;
  align-items: center;
  gap: 30px;
  flex-direction: row;
}

.nav-link {
  text-decoration: none;
  color: rgba(242, 242, 242, 0.44);
  font-weight: bold;
  font-size: 22px;
  transition: color 0.3s;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.nav-link:hover,
.nav-link.active {
  color: white;
}

.dropdown {
  position: relative;
}

.dropdown-toggle {
  cursor: pointer;
}

.dropdown-menu {
  position: absolute;
  top: 100%;
  background: white;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  border-radius: 6px;
  min-width: 140px;
  display: none;
  z-index: 1000;
}

.dropdown:hover .dropdown-menu {
  display: block;
}

.dropdown-menu a {
  display: block;
  padding: 10px 20px;
  color: #333;
  text-decoration: none;
  font-size: 14px;
  transition: background-color 0.3s;
}

.dropdown-menu a:hover {
  background-color: #f5f5f5;
}

.nav-link,
.dropdown-toggle {
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 8px;
  transition: background-color .2s ease, color .2s ease, transform .05s ease;
}

.nav-link:hover,
.dropdown-toggle:hover {
  background-color: rgba(76, 175, 80, 0.08);
}

.nav-link:active,
.dropdown-toggle:active {
  background-color: rgba(209, 209, 209, 0.16);
  transform: translateY(1px);
}

.dropdown-menu span {
  display: block;
  padding: 10px 20px;
  color: #333;
  font-size: 14px;
  cursor: pointer;
  transition: background-color .2s ease, color .2s ease;
}

.dropdown-menu span:hover {
  background-color: #f5f5f5;
}

.dropdown-menu span:active {
  background-color: #e8f5e9;
}


.search-box {
  margin-right: 20px;
  position: relative;
  display: flex;
  align-items: center;
  background: white;
  border-radius: 18px;
  border: 1px solid dodgerblue;
  overflow: hidden;
}

.search-icon {
  position: absolute;
  left: 15px;
  color: #999;
  font-size: 14px;
  z-index: 1;
}

.search-input {
  background-color: #1a252f;
  flex: 1;
  padding: 0 150px 0 34px;
  border: none;
  outline: none;
  font-size: 13px;
  color: #333;
  height: 100%;
  line-height: 1;
}

.search-input::placeholder {
  color: #999;
}

.search-type-selector {
  position: absolute;
  right: 68px;
  z-index: 2;
}

.search-type-selector :deep(.el-dropdown-link:focus) {
  border: none;
  box-shadow: none;
  outline: none;
}

.custom-select option {
  background: white;
  color: #333;
  padding: 8px 12px;
}

.select-arrow svg {
  transition: transform 0.2s ease;
}

.custom-select:focus + .select-arrow svg {
  transform: rotate(180deg);
}

.search-btn {
  position: absolute;
  right: 0;
  background: dodgerblue;
  color: white;
  border: none;
  padding: 0 15px;
  border-radius: 0 18px 18px 0;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background-color 0.3s;
  min-width: 68px;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-btn:hover {
  background: dodgerblue;
}

/* 购物车图标样式 */
.cart-icon {
  display: flex;
  position: relative;
}

.cart-icon a {
  text-decoration: none;
  color: #333;
  font-size: 22px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  transition: background-color 0.3s;
}

.cart-icon a:hover {
  background-color: #f5f5f5;
}


@media (max-width: 1024px) {
}

@media (max-width: 768px) {
  .navbar {
    height: 60px;
    width: 100%;
  }

  .menu-toggle {
    display: inline-flex;
  }

  .logo {
    display: none;
  }

  .search-box {
    margin-right: 10px;
    width: 200px;
  }

  .search-input {
    padding: 0 120px 0 30px;
    font-size: 12px;
  }
  .search-input::placeholder {
    color: transparent;
  }

  .search-type-selector {
    right: 50px;
  }

  .search-btn {
    padding: 0 12px;
    min-width: 50px;
    font-size: 12px;
  }

  .nav-links {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    background: #fff;
    flex-direction: column;
    align-items: stretch;
    gap: 0;
    border-top: 1px solid #eee;
    box-shadow: 0 8px 16px rgba(0, 0, 0, 0.08);
    transform: translateY(-12px);
    opacity: 0;
    pointer-events: none;
    transition: all .25s ease;
  }

  .nav-links.open {
    transform: translateY(0);
    opacity: 1;
    pointer-events: auto;
  }

  .nav-link, .dropdown-toggle {
    padding: 12px 16px;
    font-size: 15px;
  }

  .dropdown {
    position: static;
  }

  .dropdown-menu {
    position: static;
    box-shadow: none;
    margin: 0;
    padding: 0 0 8px;
  }

  .dropdown:hover .dropdown-menu {
    display: block;
  }
}

@media (max-width: 480px) {
  .navbar {
    padding: 0 10px;
  }

  .search-box {
    width: 160px;
    margin-right: 8px;
  }

  .search-input {
    padding: 0 100px 0 25px;
    font-size: 11px;
  }
  .search-input::placeholder {
    color: transparent;
  }

  .search-type-selector {
    right: 45px;
  }

  .search-btn {
    min-width: 45px;
    font-size: 11px;
    padding: 0 8px;
  }

  .nav-link {
    font-size: 18px;
  }
}
</style>
